* create table 3 (output as table_3_lev.tex)

clear
set more off
estimates drop _all

* ------------------------------------------------------------------------------
* set paths
global root "[include path here]"
global input "$root/Input"
global output "$root/Output"
global intermediate "$root/Intermediate"
global figures "$root/Figures"


* ==============================================================================
* READ IN STER FILES

capture program drop readster
program define readster, nclass
syntax, panel(string) dv(string) uFE(string) tFE(string) sdesc(string) ///
[csmple(string)] [opt(string)]

if "`csmple'" == "match" {
	local prefix "M"
	}
if "`dv'" == "lev" {
	local depvar "wuse"
	}
if "`dv'" == "log" {
	local depvar "log_wuse"
	}
if "`uFE'" == "p" { // p refers to parcel fixed effects
	local ptog "yes"
	local pmoctog "-"
	}
if "`uFE'" == "pm" { // pm refers to parcel by month of calendar fixed effects
	local ptog "-"
	local pmoctog "yes"
	}
if "`tFE'" == "ym" { // year month fixed fixed effects (month of sample)
	local ttog "yes"
	local tc2tog "-"
	}
if "`tFE'" == "ymc2" { // year month by cohort fixed effects
	local ttog "-"
	local tc2tog "yes"
	}
if "`csmpl'" != "match" & "`opt'" == "baltru" {
	local tFE "ymc2"
	}
estimates use "$output/did`csmple'_`depvar'_`uFE'FE`tFE'FE`opt'.ster"
estadd local pnlid "`panel'", replace
estadd local smplid "`sdesc'", replace
estadd local FEname "", replace
estadd local p "`ptog'", replace
estadd local pmoc "`pmoctog'", replace
estadd local t "`ttog'", replace
estadd local tc2 "`tc2tog'", replace
eststo `prefix'`dv'`uFE'`tFE'`opt'

end

local dependent "lev log"
foreach i of local dependent {
********************************************************************************
// read in full sample results
readster, panel("Full") dv("`i'") uFE("pm") tFE("ymc2") sdesc("full")
readster, panel("Full") dv("`i'") uFE("pm") tFE("ymc2") sdesc("1yr window") opt("1yrD")
readster, panel("Full") dv("`i'") uFE("pm") tFE("ymc2") sdesc("2yr window") opt("2yrD")
readster, panel("Full") dv("`i'") uFE("pm") tFE("ym")   sdesc("balanced") opt("baltru")
readster, panel("Full") dv("`i'") uFE("pm") tFE("ymc2") sdesc("no zero") opt("noZero")
readster, panel("Full") dv("`i'") uFE("pm") tFE("ymc2") sdesc("full") opt("tau")
readster, panel("Full") dv("`i'") uFE("pm") tFE("ymc2") sdesc("full") opt("tau2")
foreach year of numlist 2004/2009 {
readster, panel("Full") dv("`i'") uFE("pm") tFE("ymc2") sdesc("\(\le\)`year'") opt("le`year'")
}
readster, panel("Full") dv("`i'") uFE("pm") tFE("ymc2") sdesc("proxy") opt("proxy")
readster, panel("Full") dv("`i'") uFE("pm") tFE("ymc2") sdesc("full") opt("minconv")
readster, panel("Full") dv("`i'") uFE("pm") tFE("ymc2") sdesc("full (no pre)") opt("noPre")
readster, panel("Full") dv("`i'") uFE("pm") tFE("ymc2") sdesc("full") opt("ppsf")
readster, panel("Full") dv("`i'") uFE("pm") tFE("ymc2") sdesc("no early exits") opt("ex0")
readster, panel("Full") dv("`i'") uFE("pm") tFE("ymc2") sdesc("participants") opt("onlyEnroll")

********************************************************************************
// read in matched sample results
readster, panel("Matched") csmple("match") dv("`i'") uFE("pm") tFE("ym") sdesc("full")
readster, panel("Matched") csmple("match") dv("`i'") uFE("pm") tFE("ym") sdesc("1yr window") opt("1yrD")
readster, panel("Matched") csmple("match") dv("`i'") uFE("pm") tFE("ym") sdesc("2yr window") opt("2yrD")
readster, panel("Matched") csmple("match") dv("`i'") uFE("pm") tFE("ym") sdesc("no zero") opt("noZero")
readster, panel("Matched") csmple("match") dv("`i'") uFE("pm") tFE("ym") sdesc("full") opt("tau")
readster, panel("Matched") csmple("match") dv("`i'") uFE("pm") tFE("ym") sdesc("full") opt("tau2")
foreach year of numlist 2004/2009 {
readster, panel("Matched") csmple("match") dv("`i'") uFE("pm") tFE("ym") sdesc("\(\le\)`year'") opt("le`year'")
}
readster, panel("Matched") csmple("match") dv("`i'") uFE("pm") tFE("ym") sdesc("proxy") opt("proxy")
readster, panel("Matched") csmple("match") dv("`i'") uFE("pm") tFE("ym") sdesc("full") opt("minconv")
readster, panel("Matched") csmple("match") dv("`i'") uFE("pm") tFE("ym") sdesc("full (no pre)") opt("noPre")
readster, panel("Matched") csmple("match") dv("`i'") uFE("pm") tFE("ym") sdesc("full") opt("ppsf")
}
********************************************************************************

* ==============================================================================
* CREATE TABLES

// DEFINE LOCAL MACROS
local tblOP "se staraux nomti lines nonotes starlevels(* 0.10 ** 0.05 *** 0.01)"
local blev "b(%9.2f %9.3f %8.1e)"
local blog "b(%9.2f %8.1e)"
local blevOther "b(%9.2f)"
local blogOther "b(%9.2f)"

local space "\hspace{1mm}"
local par "`space' \(\mu_i\)"
local pm "`space' \(\mu_{im}\)"
local ym "`space' \(\delta_t\)"
local ymc "`space' \(\delta_{tc}\)"
local unit "Parcels"
local rowtimain "coeflabels(chi post-enroll)"

local notestar "\sym{*}\(p<0.10\), \sym{**}\(p<0.05\), \sym{***}\(p<0.01\)"
local noteSE "Parcel clustered standard errors (reported in parentheses)."
local notemu "\(\mu_{im}\) parcel by month-of-calendar year fixed-effects."
local notet "\(\delta_{t},\delta_{tc}\) month-of-sample and month-of-sample by cohort fixed-effects."
local notecohort "Cohorts based on parcel's first year in sample: 88-89, 90-94, 95-99, 00-04, 05-14."
local note2yrwin "2yr window: drop participant observations within 2 years around enrollment."
local notebaltru2 "balanced: includes parcels with non-missing water use in each month from Jan. 1988 to Apr. 2014."
local noteZero2 "no zero: includes parcels with all positive water use only."
local noteproxy "Proxy enrollment date applied 5 years prior to enrollment: sample limited to pre-enrollment dates."
local notenopre "(no pre): Model excludes pre-enrollment indicator."
local notePart "participants: includes only rebate program participants."
local noteppsf "sqft: indicator for enrollment after June 2000."
local noteminc "min conv: indicator for enrollment after Feb. 2004."

local dependent "lev log"
foreach i of local dependent {
* table of main results with robustness checks on sample:
esttab ///
`i'pmymc2 ///
`i'pmymc22yrD ///
`i'pmymc2baltru ///
`i'pmymc2noZero ///
`i'pmymc2tau2 ///
M`i'pmym ///
M`i'pmym2yrD ///
M`i'pmymnoZero ///
M`i'pmymtau2 ///
using "$output/table_3_`i'.tex", replace keep(chi chiBYtau chiBYtau2) ///
`tblOP' `b`i''  substitute(\_ _) ///
stats(pnlid smplid FEname pmoc t tc2 r2_a N_clust N, ///
fmt(%15s %15s %15s %15s %15s %15s %12.2f %12.0fc %12.0fc) ///
label("Panel" "Sample" "\emph{Fixed-effects}" "`pm'" "`ym'" "`ymc'" ///
"adj. \emph{R}$^2$" "`unit'" "Observations")) ///
coeflabels(chi "post-enroll" chiBYtau "post-enroll\(\times T_{it}\)" ///
chiBYtau2 "post-enroll\(\times T^2_{it}\)") ///
addnotes("`notestar'" "`noteSE'" "`notemu'" "`notet'" "`notecohort'" ///
"`note2yrwin'" "`notebaltru2'" "`noteZero2'")

* table of additional results:
esttab ///
`i'pmymc2proxy ///
`i'pmymc2noPre ///
`i'pmymc2onlyEnroll ///
`i'pmymc2ppsf ///
`i'pmymc2minconv ///
M`i'pmymproxy ///
M`i'pmymnoPre ///
M`i'pmymppsf ///
M`i'pmymminconv ///
using "$output/save_other-paper_`i'.tex", ///
`b`i'Other' replace `tblOP' substitute(\_ _) ///
keep(chi proxy_chi chiBYppsf chiBYminconv) ///
stats(pnlid smplid FEname pmoc t tc2 r2_a N_clust N, ///
fmt(%15s %15s %15s %15s %15s %15s %12.2f %12.0fc %12.0fc) ///
label("Panel" "Sample" "\emph{Fixed-effects}" "`pm'" "`ym'" "`ymc'" ///
"adj. \emph{R}$^2$" "`unit'" "Observations")) ///
coeflabels(chi "post-enroll" proxy_chi "proxy post-enroll" chiBYppsf ///
"post-enroll\(\times\)sqft" chiBYminconv "post-enroll\(\times\)min conv") ///
addnotes("`notestar'" "`noteSE'" "`notemu'" "`notet'" "`notecohort'" ///
"`noteproxy'" "`notenopre'" "`notePart'" "`noteppsf'" "`noteminc'")
}